//
//  TestJSObject.m
//  BaseProject
//
//  Created by lzy on 16/7/6.
//  Copyright © 2016年 zhp. All rights reserved.
//

#import "TestJSObject.h"
#import "SecurityUtil.h"
#define KEY @"anjiebang2017329" //key可修改
@implementation TestJSObject
-(void)jumpApp:(NSString *)str{
    NSLog(@"blackstr %@",str);
    if (self.jumpAppBlock) {
        self.jumpAppBlock(str);
    }
}
-(void)goBack
{
    if (self.goBackBlock) {
        self.goBackBlock();
    }
}
-(void)selectedPic:(NSArray *)type
{
    if (self.selectedPicBlock) {
        self.selectedPicBlock(type);
    }
}
-(void)sharePic:(NSArray *)type
{
    if (self.sharePicBlock) {
        self.sharePicBlock(type);
    }
}
-(void)toCredit:(int)type
{
    if (self.adTypeBlock) {
        self.adTypeBlock(type);
    }
}
-(void)share:(NSArray *)arr
{
    if (self.shareBlock) {
        self.shareBlock(arr);
    }
}
-(void)back
{
    if (self.backBlock) {
        self.backBlock();
    }
}
-(void)service:(NSString *)str
{
    if (self.serviceBlock) {
        self.serviceBlock(str);
    }
}
-(void)onlineAss
{
    if (self.onlineAssBlock) {
        self.onlineAssBlock();
    }

}
- (NSString *)getHeader
{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    NSData *d = [SecurityUtil encryptAESData:[NSString stringWithFormat:@"%@%@",UserInfoManage.userInfo.userId,dateString] app_key:KEY];
    NSString * ss = [NSString stringWithFormat:@"%@",d];
    NSString *strUrl = [ss stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *strUrl1 = [strUrl stringByReplacingOccurrencesOfString:@"<" withString:@""];
    NSString *strUrl2 = [strUrl1 stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"strUrl2 %@ %@",dateString,strUrl2);
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"ios" forKey:@"os"];
    [params setObject:SystemVersion forKey:@"osVersion"];
    [params setObject:APP_VERSION forKey:@"appVersion"];
    [params setObject:@"2.0.0" forKey:@"serviceVersion"];
    [params setObject:strUrl2 forKey:@"sign"];
    [params setObject:[NSString stringWithFormat:@"%@",dateString] forKey:@"timestamp"];
    
    if (UserInfoManage.userInfo.userId)
    {
        [params setObject:UserInfoManage.userInfo.userId forKey:@"userId"];
        [params setObject:UserInfoManage.userInfo.cityId forKey:@"cityId"];
    }
    NSDictionary * d1 = [params copy];
    
    return [self getJsonWith:d1];
}
-(NSString*)getJsonWith:(NSDictionary*)dic {
    
    NSString *json = nil;
    
    if ([NSJSONSerialization isValidJSONObject:dic]) {
        
        NSError *error;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
        
        if(!error) {
            
            json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            
        }
        
        else {
            
            NSLog(@"JSON parse error: %@", error);
            
        }
        
    }
    
    else {
        
        NSLog(@"Not a valid JSON object: %@", dic);
        
    }
    NSLog(@"json %@",json);
    return json;
    
}
-(void)backFirstSheet
{
    if (self.backFirstSheetBlock) {
        self.backFirstSheetBlock();
    }
}

-(void)tagOfService:(NSArray *)array
{
    if (self.serviceBlock) {
        self.serviceBlock(@"");
    }

}
//block
-(void)incoming:(NSArray *)str
{
    if (self.incomingBlock) {
        self.incomingBlock(str);
    }
}
-(void)question
{
    if (self.questionBlock) {
        self.questionBlock();
    }
}
-(void)ransomFloor
{
    if (self.shulouBlock) {
        self.shulouBlock();
    }
}
-(void)savePic:(NSString *)url
{
    if (self.savePicBlock) {
        self.savePicBlock(url);
    }
}
@end
